package com.oocl.admin_platform.service.impl;

import java.util.List;

import org.apache.log4j.Logger;

import com.oocl.admin_platform.dao.FailReasonDao;
import com.oocl.admin_platform.dao.MerchantDao;
import com.oocl.admin_platform.dao.factory.DaoFactory;
import com.oocl.admin_platform.pojo.FailReason;
import com.oocl.admin_platform.pojo.Merchant;
import com.oocl.admin_platform.service.MerchantService;


public class MerchantServiceImpl implements MerchantService {

	private MerchantDao merchantDao;
	
	private FailReasonDao failReasonDao;
	
	private static Logger logger=Logger.getLogger(MerchantServiceImpl.class);
	
	

	public void setFailReasonDao(FailReasonDao failReasonDao) {
		this.failReasonDao = failReasonDao;
	}
	
	public void setMerchantDao(MerchantDao merchantDao) {
		this.merchantDao = merchantDao;
	}


	public MerchantServiceImpl() {
		// TODO Auto-generated constructor stub
		merchantDao=DaoFactory.getMerchantDaoInstance();
		failReasonDao=DaoFactory.getFailReasonDaoInstance();
		
	}
	
	
	@Override
	public Merchant findByuname(String uname) {
		// TODO Auto-generated method stub
		return merchantDao.findByuname(uname);
	}

	@Override
	public List<Merchant> findByStatus(int status) {
		// TODO Auto-generated method stub
		return merchantDao.findByStatus(status);
	}

	@Override
	public boolean save(Merchant merchant) {
		// TODO Auto-generated method stub
		return merchantDao.save(merchant);
	}

	@Override
	public boolean update(Merchant merchant) {
		// TODO Auto-generated method stub
		return merchantDao.update(merchant);
	}


	@Override
	public boolean updateJMSMsg(Merchant merchant) {
		// TODO Auto-generated method stub
		boolean isSuccess=true;
		if (merchantDao.findByuname(merchant.getUname())!=null) {
			logger.info("found the same uname and update");
			isSuccess=merchantDao.update(merchant);
		}else{
			logger.info("not found the uname and save");
			isSuccess=save(merchant);
		}
		return isSuccess;
	}


	@Override
	public List<Merchant> findAll() {
		// TODO Auto-generated method stub
		return merchantDao.findAll();
	}

	@Override
	public Merchant findById(String id) {
		// TODO Auto-generated method stub
		return merchantDao.findById(id);
	}

	@Override
	/**
	 * step1:get the merchant object
	 * step2:if the status is 1 -->select the fail reason to remove the record
	 * stepe:change the merchant status
	 */
	public boolean changeStatus(String id,String status) {
		// TODO Auto-generated method stub
		Merchant merchant=merchantDao.findById(id);
		merchant.setStatus(Integer.parseInt(status));
		if ("1".equals(status)) {
			FailReason failReason=failReasonDao.findByMerchantId(id);
			if (failReason!=null) {
				boolean b=failReasonDao.delete(id);
				if (b) {
					merchantDao.update(merchant);
				}
			}else{
				merchantDao.save(merchant);
			}
		}
		return merchantDao.update(merchant);
	}

	

	

}
